<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 5.2.5: Mixed strategies for matrix games (LP formulation)</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch05_duality/html/matrix_games_LP.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 5.2.5: Mixed strategies for matrix games (LP formulation)</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 08/24/05</span>
<span class="comment">%</span>
<span class="comment">% Player 1 wishes to choose u to minimize his expected payoff u'Pv, while</span>
<span class="comment">% player 2 wishes to choose v to maximize u'Pv, where P is the payoff</span>
<span class="comment">% matrix, u and v are the probability distributions of the choices of each</span>
<span class="comment">% player (i.e. u&gt;=0, v&gt;=0, sum(u_i)=1, sum(v_i)=1)</span>
<span class="comment">% LP formulation:   minimize    t</span>
<span class="comment">%                       s.t.    u &gt;=0 , sum(u) = 1, P'*u &lt;= t*1</span>
<span class="comment">%                   maximize    t</span>
<span class="comment">%                       s.t.    v &gt;=0 , sum(v) = 1, P*v &gt;= t*1</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,0);
n = 12;
m = 12;
P = randn(n,m);

<span class="comment">% Optimal strategy for Player 1</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 1 ... '</span>);

cvx_begin
    variables <span class="string">u(n)</span> <span class="string">t1</span>
    minimize ( t1 )
    u &gt;= 0;
    ones(1,n)*u == 1;
    P'*u &lt;= t1*ones(m,1);
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Optimal strategy for Player 2</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 2 ... '</span>);

cvx_begin
    variables <span class="string">v(m)</span> <span class="string">t2</span>
    maximize ( t2 )
    v &gt;= 0;
    ones(1,m)*v == 1;
    P*v &gt;= t2*ones(n,1);
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The optimal strategies for players 1 and 2 are respectively: '</span>);
disp([u v]);
disp(<span class="string">'The expected payoffs for player 1 and player 2 respectively are: '</span>);
[t1 t2]
disp(<span class="string">'They are equal as expected!'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal strategy for player 1 ...  
Calling sedumi: 25 variables, 13 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 1 free variables
eqs m = 13, order n = 27, dim = 27, blocks = 1
nnz(A) = 192 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.33E+00 0.000
  1 :  -3.23E-01 3.71E+00 0.000 0.4455 0.9000 0.9000   2.42  1  1  6.7E+00
  2 :   1.15E-02 1.61E+00 0.000 0.4343 0.9000 0.9000   4.60  1  1  1.0E+00
  3 :   3.84E-02 4.33E-01 0.000 0.2688 0.9000 0.9000   1.51  1  1  2.2E-01
  4 :   4.33E-02 8.56E-02 0.000 0.1975 0.9000 0.9000   1.12  1  1  4.3E-02
  5 :   4.47E-02 6.91E-03 0.000 0.0808 0.9900 0.9900   1.03  1  1  3.6E-03
  6 :   4.48E-02 5.43E-05 0.000 0.0079 0.9990 0.9825   1.01  1  1  
iter seconds digits       c*x               b*y
  6      0.0   Inf  4.4840422221e-02  4.4840422221e-02
|Ax-b| =   1.7e-16, [Ay-c]_+ =   1.2E-16, |x|=  8.4e-01, |y|=  4.4e-01

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 5.12072.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
Done! 
Computing the optimal strategy for player 2 ...  
Calling sedumi: 25 variables, 13 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 1 free variables
eqs m = 13, order n = 27, dim = 27, blocks = 1
nnz(A) = 192 + 0, nnz(ADA) = 169, nnz(L) = 91
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            8.33E+00 0.000
  1 :  -3.47E-01 3.67E+00 0.000 0.4406 0.9000 0.9000   2.44  1  1  6.6E+00
  2 :  -4.31E-02 1.64E+00 0.000 0.4457 0.9000 0.9000   4.60  1  1  1.1E+00
  3 :  -4.52E-02 4.48E-01 0.000 0.2738 0.9000 0.9000   1.52  1  1  2.3E-01
  4 :  -4.50E-02 8.38E-02 0.000 0.1872 0.9000 0.9000   1.13  1  1  4.2E-02
  5 :  -4.49E-02 7.41E-03 0.000 0.0884 0.9900 0.9900   1.03  1  1  3.9E-03
  6 :  -4.48E-02 1.39E-05 0.000 0.0019 0.9990 0.9990   1.01  1  1  
iter seconds digits       c*x               b*y
  6      0.0   Inf -4.4840422221e-02 -4.4840422221e-02
|Ax-b| =   3.8e-16, [Ay-c]_+ =   1.2E-16, |x|=  1.0e+00, |y|=  4.0e-01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    2.000E-02    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.40372.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
Done! 
------------------------------------------------------------------------
The optimal strategies for players 1 and 2 are respectively: 
    0.2695    0.0686
    0.0000    0.1619
    0.0973    0.0000
    0.1573    0.2000
    0.1145   -0.0000
    0.0434    0.1545
   -0.0000    0.1146
    0.0000   -0.0000
    0.2511    0.1030
    0.0670   -0.0000
   -0.0000   -0.0000
    0.0000    0.1974

The expected payoffs for player 1 and player 2 respectively are: 

ans =

   -0.0448   -0.0448

They are equal as expected!
</pre>
</div>
</body>
</html>